package com.nidu.demo.oauth2.gatewayimpl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.cola.dto.PageQuery;
import com.alibaba.cola.dto.PageResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nidu.demo.oauth2.convertor.OAuth2ApproveConvertor;
import com.nidu.demo.oauth2.gateway.OAuth2ApproveGateway;
import com.nidu.demo.oauth2.mapper.OAuth2ApproveMapper;
import com.nidu.demo.oauth2.mapper.dataobject.OAuth2ApproveDO;
import com.nidu.demo.oauth2.model.OAuth2Approve;
import com.nidu.demo.oauth2.model.OAuth2ApproveCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Set;
import java.util.List;

@Component
@RequiredArgsConstructor
public class OAuth2ApproveGatewayImpl implements OAuth2ApproveGateway {

    private final OAuth2ApproveMapper mapper;
    private final OAuth2ApproveConvertor convertor;

    @Override
    public OAuth2Approve create(OAuth2Approve approve) {
        OAuth2ApproveDO approveDO = convertor.toDataObject(approve);
        mapper.insert(approveDO);
        return convertor.toDomainObject(approveDO);
    }

    @Override
    public OAuth2Approve update(OAuth2Approve approve) {
        OAuth2ApproveDO approveDO = convertor.toDataObject(approve);
        mapper.updateById(approveDO);
        return convertor.toDomainObject(approveDO);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteById(id);
    }

    @Override
    public void deleteByIds(Set<Long> ids) {
        mapper.deleteByIds(ids);
    }

    @Override
    public OAuth2Approve getById(Long id) {
        OAuth2ApproveDO approveDO = mapper.selectById(id);
        if (approveDO == null) {
            return null;
        }
        return convertor.toDomainObject(approveDO);
    }

    @Override
    public List<OAuth2Approve> getByUserIdAndClientId(Long userId, Long clientId) {
        LambdaQueryWrapper<OAuth2ApproveDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OAuth2ApproveDO::getUserId, userId);
        wrapper.eq(OAuth2ApproveDO::getClientId, clientId);
        List<OAuth2ApproveDO> approveDOS = mapper.selectList(wrapper);
        if (CollUtil.isEmpty(approveDOS)) {
            new ArrayList<>();
        }
        return convertor.toDomainObjects(approveDOS);
    }

    @Override
    public List<OAuth2Approve> listByCondition(OAuth2ApproveCriteria criteria) {
        LambdaQueryWrapper<OAuth2ApproveDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(!CollUtil.isEmpty(criteria.getApproveIds()), OAuth2ApproveDO::getId, criteria.getApproveIds());

        wrapper.eq(criteria.getClientId() != null, OAuth2ApproveDO::getClientId, criteria.getClientId());
        wrapper.eq(criteria.getUserId() != null, OAuth2ApproveDO::getUserId, criteria.getUserId());

        List<OAuth2ApproveDO> approveDOS = mapper.selectList(wrapper);
        if (CollUtil.isEmpty(approveDOS)) {
            return new ArrayList<>();
        }
        return convertor.toDomainObjects(approveDOS);
    }

    @Override
    public PageResponse<OAuth2Approve> pageByCondition(OAuth2ApproveCriteria criteria) {
        // 创建查询条件
        LambdaQueryWrapper<OAuth2ApproveDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(!CollUtil.isEmpty(criteria.getApproveIds()), OAuth2ApproveDO::getId, criteria.getApproveIds());

        LocalDateTime[] createTime = criteria.getCreateTime();
        if (createTime != null) {
            if (createTime.length == 1 && createTime[0] != null) {
                wrapper.ge(OAuth2ApproveDO::getCreateTime, createTime[0]);
            } else if (createTime.length > 1 && createTime[0] != null && createTime[1] != null) {
                wrapper.between(OAuth2ApproveDO::getCreateTime, createTime[0], createTime[1]);
            }
        }

        wrapper.eq(criteria.getClientId() != null, OAuth2ApproveDO::getClientId, criteria.getClientId());
        wrapper.eq(criteria.getUserId() != null, OAuth2ApproveDO::getUserId, criteria.getUserId());

        // 创建分页对象，确保使用 OAuth2ApproveDO
        Page<OAuth2ApproveDO> page = new Page<>(criteria.getPageIndex(), criteria.getPageSize());

        // 排序字段
        if (!StrUtil.isEmpty(criteria.getOrderBy())) {
            if (PageQuery.DESC.equals(criteria.getOrderDirection())) {
                page.addOrder(OrderItem.desc(criteria.getOrderBy()));
            } else {
                page.addOrder(OrderItem.asc(criteria.getOrderBy()));
            }
        }

        mapper.selectPage(page, wrapper);
        List<OAuth2Approve> approves = convertor.toDomainObjects(page.getRecords());
        return PageResponse.of(approves, (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
    }
}